package org.fuys.coder.common.util;

import org.fuys.coder.common.constants.ResultMessageConstants;
import org.fuys.coder.common.exception.BusinessException;

import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
 * @projectName: fuys-low-coder
 * @package: org.fuys.coder.common.util
 * @className: DateUtil
 * @author: WangJing
 * @description: 时间工具类
 * @date: 2024/8/9 16:49
 * @version: 1.0
 */
public class DateUtil {

    public static Date addTimeFromCur(int time,TimeUnit timeUnit){
        final long curTime = new Date().getTime();
        long period=time;
        switch (timeUnit){
            case DAYS: {
                period = period * 24 * 60 * 60 * 1000;
                break;
            }
            case HOURS:{
                period = period * 60 * 60 * 1000;
                break;
            }
            default:
                throw new BusinessException(ResultMessageConstants.ILLEGAL_DATA_FORMAT+"无法计算的时间单位:"+timeUnit);
        }
        return new Date(curTime+period);
    }

    public static Date addTimeFrom(Date date, int time, TimeUnit timeUnit){
        final long dateTime = date.getTime();
        long period=time;
        switch (timeUnit){
            case DAYS: {
                period = period * 24 * 60 * 60 * 1000;
                break;
            }
            case HOURS:{
                period = period * 60 * 60 * 1000;
                break;
            }
            default:
                throw new BusinessException(ResultMessageConstants.ILLEGAL_DATA_FORMAT+"无法计算的时间单位:"+timeUnit);
        }
        return new Date(dateTime+period);
    }
}
